package com.onlyphoto.onlymall.mvp.view.binder

import android.annotation.SuppressLint
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.OrderDetailsHeaderBean
import com.onlyphoto.onlymall.mvp.view.activity.LogisticsActivity
import com.onlyphoto.onlymall.mvp.view.activity.OrderDetailsActivity
import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-08-26.
 */
class OrderDetailsHeaderBinder : ItemViewBinder<OrderDetailsHeaderBean, OrderDetailsHeaderBinder.ViewHolder>() {

    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.item_order_details_header, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, bean: OrderDetailsHeaderBean) {
        viewHolder.update(bean)

    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        private var back :ImageView? = null
        var state :TextView? = null
        private var clLogistics : ConstraintLayout? = null
        var name: TextView? = null
        var phone: TextView? = null
        var address: TextView? = null

        init {
            back = itemView.findViewById(R.id.back)
            state = itemView.findViewById(R.id.state)
            name = itemView.findViewById(R.id.name)
            phone = itemView.findViewById(R.id.phone)
            address = itemView.findViewById(R.id.address)
            clLogistics = itemView.findViewById(R.id.cl_logistics)
        }

        @SuppressLint("SetTextI18n")
        fun update(bean: OrderDetailsHeaderBean) {
            if (bean.state == 1 && bean.stateExtra == 0) {
                state?.text = "等待付款"
            }

            when (bean.state) {
                2 ->
                    state?.text = "待发货"
                3 -> {
                    state?.text = "待收货"
                    clLogistics?.visibility = View.VISIBLE
                }
                5 ->
                    state?.text = "已完成"
            }

            when (bean.stateExtra) {
                10 ->
                    state?.text = "申请退货中"
                11 ->
                    state?.text = "退货被驳回"
                12 ->
                    state?.text = "同意退货"
                13 ->
                    state?.text = "退货成功"
                14 ->
                    state?.text = "交易关闭"
                15 ->
                    state?.text = "交易关闭"
            }
            back?.setOnClickListener{
                (itemView.context as OrderDetailsActivity).finish()
            }
            clLogistics?.setOnClickListener {
                itemView.context.startActivity(Intent(itemView.context, LogisticsActivity::class.java)
                        .putExtra("expressCompanyPy", bean.expressCompanyPy)
                        .putExtra("expressNum", bean.expressNum))
            }

            name?.text = bean.name
            phone?.text = bean.phone
            address?.text = bean.address
        }

    }
}
